using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Configuration;
using PCSComUtils.DataAccess;
using PCSComUtils.PCSExc;
using PCSComUtils.Common;
using System.Runtime.InteropServices;
namespace PCSComProduction.ShopFloorControl.DS
{
	public class PRO_DispatchDetailDS  
	{
		public PRO_DispatchDetailDS()
		{
		}
		private const string THIS = "PCSComProduction.ShopFloorControl.DS.PRO_DispatchDetailDS";
		//**************************************************************************              
		///    <Description>
		///       This method uses to add data to PRO_DispatchDetail
		///    </Description>
		///    <Inputs>
		///        PRO_DispatchDetailVO       
		///    </Inputs>
		///    <Outputs>
		///       newly inserted primarkey value
		///    </Outputs>
		///    <Returns>
		///       void
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Tuesday, May 31, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************


		public void Add(object pobjObjectVO)
		{
			const string METHOD_NAME = THIS + ".Add()";
			 
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS =null;
			try
			{
				PRO_DispatchDetailVO objObject = (PRO_DispatchDetailVO) pobjObjectVO;
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);
				
				strSql=	"INSERT INTO PRO_DispatchDetail("
					+ PRO_DispatchDetailTable.STARTDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.ENDDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.SHIFTID_FLD + ","
					+ PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD + ","
					+ PRO_DispatchDetailTable.WOROUTINGID_FLD + ","
					+ PRO_DispatchDetailTable.LABORSETUPID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINESETUPID_FLD + ","
					+ PRO_DispatchDetailTable.LABORRUNID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINERUNID_FLD + ","
					+ PRO_DispatchDetailTable.LABORLOADID_FLD + ","
					+ PRO_DispatchDetailTable.RECEIVERID_FLD + ","
					+ PRO_DispatchDetailTable.TRANSFERID_FLD + ","
					+ PRO_DispatchDetailTable.PRODUCTID_FLD + ","
					+ PRO_DispatchDetailTable.DISPATCHMASTERID_FLD + ")"
					+ "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.STARTDATETIME_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.STARTDATETIME_FLD].Value = objObject.StartDateTime;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.ENDDATETIME_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.ENDDATETIME_FLD].Value = objObject.EndDateTime;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.SHIFTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.SHIFTID_FLD].Value = objObject.ShiftID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD].Value = objObject.CompletedQuantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.WOROUTINGID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.WOROUTINGID_FLD].Value = objObject.WORoutingID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.LABORSETUPID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.LABORSETUPID_FLD].Value = objObject.LaborSetupID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.MACHINESETUPID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.MACHINESETUPID_FLD].Value = objObject.MachineSetupID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.LABORRUNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.LABORRUNID_FLD].Value = objObject.LaborRunID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.MACHINERUNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.MACHINERUNID_FLD].Value = objObject.MachineRunID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.LABORLOADID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.LABORLOADID_FLD].Value = objObject.LaborLoadID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.RECEIVERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.RECEIVERID_FLD].Value = objObject.ReceiverID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.TRANSFERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.TRANSFERID_FLD].Value = objObject.TransferID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.DISPATCHMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.DISPATCHMASTERID_FLD].Value = objObject.DispatchMasterID;


				
				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
	
		//**************************************************************************              
		///    <Description>
		///       This method uses to delete data from PRO_DispatchDetail
		///    </Description>
		///    <Inputs>
		///        ID       
		///    </Inputs>
		///    <Outputs>
		///       void
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       09-Dec-2004
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		public void Delete(int pintID)
		{
			const string METHOD_NAME = THIS + ".Delete()";
			string strSql = String.Empty;
			strSql=	"DELETE " + PRO_DispatchDetailTable.TABLE_NAME + " WHERE  " + "DispatchDetailID" + "=" + pintID.ToString();
			OleDbConnection oconPCS=null;
			OleDbCommand ocmdPCS =null;
			try
			{
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	
				ocmdPCS = null;

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}



			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
	
		//**************************************************************************              
		///    <Description>
		///       This method uses to get data from PRO_DispatchDetail
		///    </Description>
		///    <Inputs>
		///        ID       
		///    </Inputs>
		///    <Outputs>
		///       PRO_DispatchDetailVO
		///    </Outputs>
		///    <Returns>
		///       PRO_DispatchDetailVO
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Tuesday, May 31, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		public object GetObjectVO(int pintID)
		{
			const string METHOD_NAME = THIS + ".GetObjectVO()";
			DataSet dstPCS = new DataSet();
			
			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				strSql=	"SELECT "
					+ PRO_DispatchDetailTable.DISPATCHDETAILID_FLD + ","
					+ PRO_DispatchDetailTable.STARTDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.ENDDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.SHIFTID_FLD + ","
					+ PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD + ","
					+ PRO_DispatchDetailTable.WOROUTINGID_FLD + ","
					+ PRO_DispatchDetailTable.LABORSETUPID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINESETUPID_FLD + ","
					+ PRO_DispatchDetailTable.LABORRUNID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINERUNID_FLD + ","
					+ PRO_DispatchDetailTable.LABORLOADID_FLD + ","
					+ PRO_DispatchDetailTable.RECEIVERID_FLD + ","
					+ PRO_DispatchDetailTable.TRANSFERID_FLD + ","
					+ PRO_DispatchDetailTable.PRODUCTID_FLD + ","
					+ PRO_DispatchDetailTable.DISPATCHMASTERID_FLD
					+ " FROM " + PRO_DispatchDetailTable.TABLE_NAME
					+" WHERE " + PRO_DispatchDetailTable.DISPATCHDETAILID_FLD + "=" + pintID;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				odrPCS = ocmdPCS.ExecuteReader();

				PRO_DispatchDetailVO objObject = new PRO_DispatchDetailVO();

				while (odrPCS.Read())
				{ 
					objObject.DispatchDetailID = int.Parse(odrPCS[PRO_DispatchDetailTable.DISPATCHDETAILID_FLD].ToString().Trim());
					objObject.StartDateTime = DateTime.Parse(odrPCS[PRO_DispatchDetailTable.STARTDATETIME_FLD].ToString().Trim());
					objObject.EndDateTime = DateTime.Parse(odrPCS[PRO_DispatchDetailTable.ENDDATETIME_FLD].ToString().Trim());
					objObject.ShiftID = int.Parse(odrPCS[PRO_DispatchDetailTable.SHIFTID_FLD].ToString().Trim());
					objObject.CompletedQuantity = Decimal.Parse(odrPCS[PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD].ToString().Trim());
					objObject.WORoutingID = int.Parse(odrPCS[PRO_DispatchDetailTable.WOROUTINGID_FLD].ToString().Trim());
					objObject.LaborSetupID = int.Parse(odrPCS[PRO_DispatchDetailTable.LABORSETUPID_FLD].ToString().Trim());
					objObject.MachineSetupID = int.Parse(odrPCS[PRO_DispatchDetailTable.MACHINESETUPID_FLD].ToString().Trim());
					objObject.LaborRunID = int.Parse(odrPCS[PRO_DispatchDetailTable.LABORRUNID_FLD].ToString().Trim());
					objObject.MachineRunID = int.Parse(odrPCS[PRO_DispatchDetailTable.MACHINERUNID_FLD].ToString().Trim());
					objObject.LaborLoadID = int.Parse(odrPCS[PRO_DispatchDetailTable.LABORLOADID_FLD].ToString().Trim());
					objObject.ReceiverID = int.Parse(odrPCS[PRO_DispatchDetailTable.RECEIVERID_FLD].ToString().Trim());
					objObject.TransferID = int.Parse(odrPCS[PRO_DispatchDetailTable.TRANSFERID_FLD].ToString().Trim());
					objObject.ProductID = int.Parse(odrPCS[PRO_DispatchDetailTable.PRODUCTID_FLD].ToString().Trim());
					objObject.DispatchMasterID = int.Parse(odrPCS[PRO_DispatchDetailTable.DISPATCHMASTERID_FLD].ToString().Trim());

				}		
				return objObject;					
			}
			catch(OleDbException ex)
			{			
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to update data to PRO_DispatchDetail
		///    </Description>
		///    <Inputs>
		///       PRO_DispatchDetailVO       
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       09-Dec-2004
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		
	
		public void Update(object pobjObjecVO)
		{
			const string METHOD_NAME = THIS + ".Update()";

			PRO_DispatchDetailVO objObject = (PRO_DispatchDetailVO) pobjObjecVO;
			

			//prepare value for parameters
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				strSql=	"UPDATE PRO_DispatchDetail SET "
					+ PRO_DispatchDetailTable.STARTDATETIME_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.ENDDATETIME_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.SHIFTID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.WOROUTINGID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.LABORSETUPID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.MACHINESETUPID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.LABORRUNID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.MACHINERUNID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.LABORLOADID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.RECEIVERID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.TRANSFERID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.PRODUCTID_FLD + "=   ?" + ","
					+ PRO_DispatchDetailTable.DISPATCHMASTERID_FLD + "=  ?"
					+" WHERE " + PRO_DispatchDetailTable.DISPATCHDETAILID_FLD + "= ?";

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.STARTDATETIME_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.STARTDATETIME_FLD].Value = objObject.StartDateTime;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.ENDDATETIME_FLD, OleDbType.Date));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.ENDDATETIME_FLD].Value = objObject.EndDateTime;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.SHIFTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.SHIFTID_FLD].Value = objObject.ShiftID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD, OleDbType.Decimal));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD].Value = objObject.CompletedQuantity;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.WOROUTINGID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.WOROUTINGID_FLD].Value = objObject.WORoutingID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.LABORSETUPID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.LABORSETUPID_FLD].Value = objObject.LaborSetupID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.MACHINESETUPID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.MACHINESETUPID_FLD].Value = objObject.MachineSetupID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.LABORRUNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.LABORRUNID_FLD].Value = objObject.LaborRunID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.MACHINERUNID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.MACHINERUNID_FLD].Value = objObject.MachineRunID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.LABORLOADID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.LABORLOADID_FLD].Value = objObject.LaborLoadID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.RECEIVERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.RECEIVERID_FLD].Value = objObject.ReceiverID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.TRANSFERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.TRANSFERID_FLD].Value = objObject.TransferID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.PRODUCTID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.PRODUCTID_FLD].Value = objObject.ProductID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.DISPATCHMASTERID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.DISPATCHMASTERID_FLD].Value = objObject.DispatchMasterID;

				ocmdPCS.Parameters.Add(new OleDbParameter(PRO_DispatchDetailTable.DISPATCHDETAILID_FLD, OleDbType.Integer));
				ocmdPCS.Parameters[PRO_DispatchDetailTable.DISPATCHDETAILID_FLD].Value = objObject.DispatchDetailID;


				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	
			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}

		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to get all data from PRO_DispatchDetail
		///    </Description>
		///    <Inputs>
		///               
		///    </Inputs>
		///    <Outputs>
		///       DataSet
		///    </Outputs>
		///    <Returns>
		///       DataSet
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Tuesday, May 31, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************

		public DataSet List()
		{
			const string METHOD_NAME = THIS + ".List()";
			DataSet dstPCS = new DataSet();
			
		

			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				strSql=	"SELECT "
					+ PRO_DispatchDetailTable.DISPATCHDETAILID_FLD + ","
					+ PRO_DispatchDetailTable.STARTDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.ENDDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.SHIFTID_FLD + ","
					+ PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD + ","
					+ PRO_DispatchDetailTable.WOROUTINGID_FLD + ","
					+ PRO_DispatchDetailTable.LABORSETUPID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINESETUPID_FLD + ","
					+ PRO_DispatchDetailTable.LABORRUNID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINERUNID_FLD + ","
					+ PRO_DispatchDetailTable.LABORLOADID_FLD + ","
					+ PRO_DispatchDetailTable.RECEIVERID_FLD + ","
					+ PRO_DispatchDetailTable.TRANSFERID_FLD + ","
					+ PRO_DispatchDetailTable.PRODUCTID_FLD + ","
					+ PRO_DispatchDetailTable.DISPATCHMASTERID_FLD
					+ " FROM " + PRO_DispatchDetailTable.TABLE_NAME;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();
				
				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS,PRO_DispatchDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch(OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}


		}


		public DataSet SearchDispatchDetail(Hashtable phashCondition)
		{
			const string METHOD_NAME = THIS + ".SearchDispatchDetail()";
			const string VIEW_DISPATCH_DETAIL = "v_PRO_DispatchDetail";
			DataSet dstPCS = new DataSet();
			
		

			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				strSql=	"SELECT *  "
					+ " FROM " + VIEW_DISPATCH_DETAIL 
					+ " WHERE 1 = 1 " ;

				if (phashCondition !=null)
				{
					IDictionaryEnumerator myEnumerator = phashCondition.GetEnumerator();
					while ( myEnumerator.MoveNext() )
					{
						strSql += " AND " + myEnumerator.Key + "'" + myEnumerator.Value + "'"; //.ToString().Replace("'","''")
					}
				}

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();
				
				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS,PRO_DispatchDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch(OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}


		}

		//**************************************************************************              
		///    <Description>
		///       This method uses to update a DataSet
		///    </Description>
		///    <Inputs>
		///        DataSet       
		///    </Inputs>
		///    <Outputs>
		///       
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       HungLa
		///    </Authors>
		///    <History>
		///       Tuesday, May 31, 2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		
		public void UpdateDataSet(DataSet pData)
		{
			const string METHOD_NAME = THIS + ".UpdateDataSet()";
			string strSql;
			OleDbConnection oconPCS =null;
			OleDbCommandBuilder odcbPCS ;
			OleDbDataAdapter odadPCS = new OleDbDataAdapter();

			try
			{
				strSql=	"SELECT "
					+ PRO_DispatchDetailTable.DISPATCHDETAILID_FLD + ","
					+ PRO_DispatchDetailTable.STARTDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.ENDDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.SHIFTID_FLD + ","
					+ PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD + ","
					+ PRO_DispatchDetailTable.WOROUTINGID_FLD + ","
					+ PRO_DispatchDetailTable.LABORSETUPID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINESETUPID_FLD + ","
					+ PRO_DispatchDetailTable.LABORRUNID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINERUNID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINELOADID_FLD + ","
					+ PRO_DispatchDetailTable.LABORLOADID_FLD + ","
					+ PRO_DispatchDetailTable.RECEIVERID_FLD + ","
					+ PRO_DispatchDetailTable.TRANSFERID_FLD + ","
					+ PRO_DispatchDetailTable.PRODUCTID_FLD + ","
					+ PRO_DispatchDetailTable.WORKCENTERID_FLD + ","
					+ PRO_DispatchDetailTable.INQUANTITY_FLD + ","
					+ PRO_DispatchDetailTable.DISPATCHMASTERID_FLD 
					+ "  FROM " + PRO_DispatchDetailTable.TABLE_NAME;

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				odadPCS.SelectCommand = new OleDbCommand(strSql, oconPCS);
				odcbPCS = new OleDbCommandBuilder(odadPCS);
				pData.EnforceConstraints = false;
				odadPCS.Update(pData,PRO_DispatchDetailTable.TABLE_NAME);

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else if (ex.Errors[1].NativeError == ErrorCode.SQLCASCADE_PREVENT_KEYCODE)
				{

					throw new PCSDBException(ErrorCode.CASCADE_DELETE_PREVENT, METHOD_NAME, ex);				
				}
				
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}

		}
		/// <summary>
		/// InsertListDispatchDetail
		/// </summary>
		/// <author>Trada</author>
		/// <date>Tuesday, June 7 2005</date>
		/// <param name="pintMasterID"></param>
		public void InsertListDispatchDetail(int pintMasterID)
		{
			const string METHOD_NAME = THIS + ".InsertListDispatchDetail()";
			 
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS =null;
			try
			{
				string strSql = String.Empty;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand("", oconPCS);
				
				strSql=	" INSERT INTO " 
					+ PRO_DispatchDetailTable.TABLE_NAME 
					+ " (" + PRO_DispatchDetailTable.WOROUTINGID_FLD + ", "
					+ PRO_DispatchDetailTable.WORKCENTERID_FLD + ", "
					+ PRO_DispatchDetailTable.PRODUCTID_FLD + ", "
					+ PRO_DispatchDetailTable.DISPATCHMASTERID_FLD + ") "
					+ " SELECT " 
					+ PRO_WORoutingTable.WOROUTINGID_FLD + ", " 
					+ PRO_WORoutingTable.WORKCENTERID_FLD + ", " 
					+ "R." + PRO_WORoutingTable.PRODUCTID_FLD + ", "
					+ PRO_DispatchMasterTable.DISPATCHMASTERID_FLD 
					+ " FROM " + PRO_WORoutingTable.TABLE_NAME + " R"
					+ " INNER JOIN " + PRO_DispatchMasterTable.TABLE_NAME + " DM" 
					+ " ON " + " R." + PRO_WORoutingTable.WORKORDERDETAILID_FLD + " = " 
					+ "DM." + PRO_DispatchMasterTable.WORKORDERDETAILID_FLD
					+ " AND " + " DM." + PRO_DispatchMasterTable.DISPATCHMASTERID_FLD + " = " + pintMasterID;
				
				ocmdPCS.CommandText = strSql;
				ocmdPCS.Connection.Open();
				ocmdPCS.ExecuteNonQuery();	

			}
			catch(OleDbException ex)
			{
				if (ex.Errors[1].NativeError == ErrorCode.SQLDUPLICATE_KEYCODE)
				{
																   
					throw new PCSDBException(ErrorCode.DUPLICATE_KEY, METHOD_NAME, ex);				
				}
				else
				{
					throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
				}
			}			

			catch(InvalidOperationException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
		/// <summary>
		/// Get Detail Dispatch data by WorkOrderDetailID
		/// </summary>
		/// <returns>Dispatch Detai data by DispatchMasterID</returns>
		/// <author>
		/// Do Manh Tuan
		/// 16-06-2005
		/// </author>
		public DataSet GetDetailDispatchData(int pintWorkOrderDetailID)
		{
			const string METHOD_NAME = THIS + ".List()";
			const string INQUANTITY = "InQuantity";
			const string TRANSFER = "Transfer";
			const string RECEIVER = "Receiver";
			DataSet dstPCS = new DataSet();
			
			OleDbConnection oconPCS =null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				
				strSql=	"SELECT "
					+ PRO_DispatchDetailTable.DISPATCHDETAILID_FLD + ","
					+ PRO_DispatchDetailTable.SHIFTID_FLD + ","
					+ PRO_DispatchDetailTable.WOROUTINGID_FLD + ","
					+ PRO_DispatchDetailTable.TABLE_NAME + "." + PRO_DispatchDetailTable.PRODUCTID_FLD + ","
					+ PRO_DispatchDetailTable.TABLE_NAME + "." + PRO_DispatchDetailTable.DISPATCHMASTERID_FLD + ","
					+ "(SELECT " + PRO_WORoutingTable.STEP_FLD + " FROM " + PRO_WORoutingTable.TABLE_NAME + " WHERE " + PRO_WORoutingTable.WOROUTINGID_FLD + " = " + PRO_DispatchDetailTable.TABLE_NAME + "." + PRO_DispatchDetailTable.WOROUTINGID_FLD + ") as " + PRO_WORoutingTable.STEP_FLD + ","
					+ "(SELECT " + PRO_ShiftTable.SHIFTDESC_FLD + " FROM " + PRO_ShiftTable.TABLE_NAME + " WHERE " + PRO_ShiftTable.SHIFTID_FLD + " = " + PRO_DispatchDetailTable.TABLE_NAME + "." + PRO_DispatchDetailTable.SHIFTID_FLD + ") as " + PRO_ShiftTable.SHIFTDESC_FLD + ","
					+ PRO_DispatchDetailTable.STARTDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.ENDDATETIME_FLD + ","
					+ PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD + ","
					+ PRO_DispatchDetailTable.LABORLOADID_FLD + ","
					+ PRO_DispatchDetailTable.LABORSETUPID_FLD + ","
					+ PRO_DispatchDetailTable.LABORRUNID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINELOADID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINESETUPID_FLD + ","
					+ PRO_DispatchDetailTable.MACHINERUNID_FLD + ","
					+ PRO_DispatchDetailTable.RECEIVERID_FLD + ","
					+ PRO_DispatchDetailTable.TRANSFERID_FLD + ","
					+ PRO_DispatchDetailTable.WORKCENTERID_FLD + ","
					+ "null as " + INQUANTITY + ","
					+ "(SELECT " + Sys_UserTable.USERNAME_FLD + " FROM " + Sys_UserTable.TABLE_NAME + " WHERE " + Sys_UserTable.USERID_FLD + " = " + PRO_DispatchDetailTable.TABLE_NAME + "." + PRO_DispatchDetailTable.TRANSFERID_FLD + ") as "  + TRANSFER + ","
					+ "(SELECT " + Sys_UserTable.USERNAME_FLD + " FROM " + Sys_UserTable.TABLE_NAME + " WHERE " + Sys_UserTable.USERID_FLD + " = " + PRO_DispatchDetailTable.TABLE_NAME + "." + PRO_DispatchDetailTable.RECEIVERID_FLD + ") as "  + RECEIVER
					+ " FROM " + PRO_DispatchDetailTable.TABLE_NAME + " INNER JOIN " + PRO_DispatchMasterTable.TABLE_NAME + " ON "
					+ PRO_DispatchDetailTable.TABLE_NAME + "." + PRO_DispatchDetailTable.DISPATCHMASTERID_FLD 
					+ " = " + PRO_DispatchMasterTable.TABLE_NAME + "." + PRO_DispatchMasterTable.DISPATCHMASTERID_FLD
					+ " WHERE " + PRO_DispatchMasterTable.WORKORDERDETAILID_FLD + " = " + pintWorkOrderDetailID;
				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);
				ocmdPCS.Connection.Open();
				
				OleDbDataAdapter odadPCS = new OleDbDataAdapter(ocmdPCS);
				odadPCS.Fill(dstPCS,PRO_DispatchDetailTable.TABLE_NAME);

				return dstPCS;
			}
			catch(OleDbException ex)
			{
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			
			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}
		/// <summary>
		/// Get Information for WO Number,WO Line,Operation,Shift,Start Date Time,End Date Time,Completed Qty,
		/// Labor Setup,Labor Run,Machine Run
		/// 
		/// select WO Number,WO Line,Operation,Shift,Start Date Time,End Date Time,Completed Qty,
		/// Labor Setup,Labor Run,Machine Run from PRO_DispatchMaster a,PRO_DispatchDetail b  where 
		/// a. DispatchMasterID=b.DispatchMasterID
		/// and a.MasterLocationID=pintMasLoc  
		/// and a.WorkCenterID = pintWorkCenterID
		/// and pintstartdate< b.Start Date Time <pintenddate
		/// and b.shiftID= pintshiftID
		/// </summary>
		public void ListDispatchInforForWorkCenter(int pintWorkCenterID, DateTime pdtmstartdate, DateTime pdtmenddate, int pintMasLoc, int pintShiftID)
		{
		}

		/// <summary>
		/// This method is used to get the completed quantity for this routing step
		/// </summary>
		/// <param name="pintWODetailID"></param>
		/// <param name="pintWORoutingID"></param>
		/// <returns></returns>
		public decimal GetCompletedQty(int pintWODetailID, int pintWORoutingID)
		{
			const string METHOD_NAME = THIS + ".GetCompletedQty()";
			DataSet dstPCS = new DataSet();
			
			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				strSql=	"SELECT "
					+ PRO_DispatchDetailTable.COMPLETEDQUANTITY_FLD 
					+ " FROM " + PRO_DispatchDetailTable.TABLE_NAME
					+ " WHERE " + PRO_DispatchDetailTable.WOROUTINGID_FLD + "=" + pintWORoutingID 
					+ "   AND " + PRO_DispatchDetailTable.DISPATCHMASTERID_FLD 
					+ "    IN ( SELECT " + PRO_DispatchMasterTable.DISPATCHMASTERID_FLD 
					+ "         FROM " + PRO_DispatchMasterTable.TABLE_NAME 
					+ "          WHERE " + PRO_DispatchMasterTable.WORKORDERDETAILID_FLD + "=" + pintWODetailID
					+ "        ) ";

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				object objResult = ocmdPCS.ExecuteScalar();
				
				if (objResult == DBNull.Value || objResult == null)
				{
					return 0;
				}
				else
				{
					return decimal.Parse(objResult.ToString());
				}
			}
			catch(OleDbException ex)
			{			
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}


		/// <summary>
		/// This method is used to get the received person for this operation
		/// </summary>
		/// <param name="pintWODetailID"></param>
		/// <param name="pintWORoutingID"></param>
		/// <returns></returns>
		public int GetReceivedPersonID(int pintWODetailID, int pintWORoutingID)
		{
			const string METHOD_NAME = THIS + ".GetCompletedQty()";
			DataSet dstPCS = new DataSet();
			
			OleDbDataReader odrPCS = null;
			OleDbConnection oconPCS = null;
			OleDbCommand ocmdPCS = null;
			try 
			{
				string strSql = String.Empty;
				strSql=	"SELECT "
					+ PRO_DispatchDetailTable.RECEIVERID_FLD 
					+ " FROM " + PRO_DispatchDetailTable.TABLE_NAME
					+ " WHERE " + PRO_DispatchDetailTable.WOROUTINGID_FLD + "=" + pintWORoutingID 
					+ "   AND " + PRO_DispatchDetailTable.DISPATCHMASTERID_FLD 
					+ "    IN ( SELECT " + PRO_DispatchMasterTable.DISPATCHMASTERID_FLD 
					+ "         FROM " + PRO_DispatchMasterTable.TABLE_NAME 
					+ "          WHERE " + PRO_DispatchMasterTable.WORKORDERDETAILID_FLD + "=" + pintWODetailID
					+ "        ) ";

				
				oconPCS = new OleDbConnection(Utils.Instance.OleDbConnectionString);
				ocmdPCS = new OleDbCommand(strSql, oconPCS);

				ocmdPCS.Connection.Open();
				object objResult = ocmdPCS.ExecuteScalar();
				
				if (objResult == DBNull.Value || objResult == null)
				{
					return 0;
				}
				else
				{
					return int.Parse(objResult.ToString());
				}
			}
			catch(OleDbException ex)
			{			
				throw new PCSDBException(ErrorCode.ERROR_DB, METHOD_NAME,ex);
			}			

			catch (Exception ex) 
			{
				throw new PCSDBException(ErrorCode.OTHER_ERROR, METHOD_NAME, ex);
			}

			finally 
			{
				if (oconPCS!=null) 
				{
					if (oconPCS.State != ConnectionState.Closed) 
					{
						oconPCS.Close();
					}
				}
			}
		}

	}
}
